<?php
class PhotoUpload{
	
	protected $errorMessage = "Invalid File Extension.";
	protected $imagePath 	= "_styles/photos/";
	protected $fileFormat	= array(1=>'jpg',2=>'jpeg',3=>'gif',4=>'png');
	
	public function getExtension($str){
		$i = strrpos($str,".");
		if(!$i){return "";}
		$l = strlen($str) - $i;
		$ext = substr($str,$i+1,$l);
		return $ext;
	}
	
	public function uploadImage($file,$locName,$locCity){
		
		$filepath = $file['name'];
		
		if($filepath){
			$filename  = stripslashes($filepath);
			$extension = strtolower($this->getExtension($filename));
			
			$arrsearch = array_search($extension,$this->fileFormat);
			if(empty($arrsearch)){
				echo $this->errorMessage;
			}
			else{
				$new_name 		= $locName."_".$locCity;
				$new_filename 	= $new_name.".".$extension;
				$new_filepath 	= $this->imagePath.$new_filename;
				$copied 		= copy($file['tmp_name'], $new_filepath);
			
				if($copied){
					return $new_filename;
				}
			}
		}
	}
}
?>